package com.yuan.web.action.feedback;

import static com.yuan.common.action.BaseAction.SUCCESS;
import jxl.common.Logger;

import com.yuan.common.action.BaseAction;
import com.yuan.common.message.InformationMessage;
import com.yuan.feedback.Feedback;
import com.yuan.service.feedback.FeedbackService;

/**
 * 
 * 反映用户反馈
 * 
 * @author Beni
 * 
 */
public class FeedbackAction extends BaseAction {

	private static final long serialVersionUID = 2920331000433731501L;

	private static final Logger logger = Logger.getLogger(FeedbackAction.class);

	private FeedbackService feedbackService;

	private Feedback feedback;

	/**
	 * 显示用户的建议画面
	 * 
	 * @return
	 */
	public String show() {
		return "suggest";
	}

	public String save() {
		try {

			if (!checkInput()) {
				return "suggest";
			}

			feedbackService.save(feedback);
			this.setInformationMessage(new InformationMessage("INF001"));

		} catch (Exception e) {
			logger.error("保存Feedback失败", e);
			this.setInformationMessage(new InformationMessage("ERR001"));
			return "suggest";
		}

		return SUCCESS;
	}

	private boolean checkInput() {
		if (feedback.getUsername() == null
				|| feedback.getUsername().trim().isEmpty()) {
			this.setInformationMessage(new InformationMessage("WAR001", "用户名"));
			return false;
		}

		if (feedback.getContact() == null
				|| feedback.getContact().trim().isEmpty()) {
			this.setInformationMessage(new InformationMessage("WAR001", "联系方式"));
			return false;
		}

		if (feedback.getContent() == null
				|| feedback.getContent().trim().isEmpty()) {
			this.setInformationMessage(new InformationMessage("WAR001", "内容"));
			return false;
		}

		return true;
	}

	public FeedbackService getFeedbackService() {
		return feedbackService;
	}

	public void setFeedbackService(FeedbackService feedbackService) {
		this.feedbackService = feedbackService;
	}

	public Feedback getFeedback() {
		return feedback;
	}

	public void setFeedback(Feedback feedback) {
		this.feedback = feedback;
	}

}
